package com.example.administrator.kotiln.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.administrator.kotiln.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * Created by Administrator on 2017/10/20.
 */

public class OkHttpActivity extends AppCompatActivity {

    private OkHttpClient mHttpClient;
    private Response response;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        initNet();
        mHttpClient = new OkHttpClient();
    }

    private void initNet() {
        String url = "http://write.blog.csdn.net/postlist/0/0/enabled/1";
        Request request = new Request.Builder().url(url).build();

        try {

            response = mHttpClient.newCall(request).execute();
            String json = response.body().string();
            Log.d("okHttp", json);
            if (response.isSuccessful()) {

            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void post(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("name", "liming")
                .add("school", "beida")
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

}

